package com.treasure.mall.zbqmallapi.controller;

import com.treasure.mall.biz.remote.wx.ZbWxMpService;
import com.treasure.mall.zbq.common.base.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallapi.controller
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/8/26 3:43 下午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * 
 */

@RestController
@RequestMapping("/wx/mp")
public class WxMpController {

    @Autowired
    private ZbWxMpService zbWxMpService;

    @GetMapping("/check")
    public String checkToken(HttpServletRequest request) {
        // 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");
        if (zbWxMpService.checkSignature(signature, timestamp, nonce)) {
            return echostr;
        }
        return "";
    }

    @PostMapping("/check")
    public String messageHandler(HttpServletRequest request, HttpServletResponse response) {
        return zbWxMpService.messageHandle(request, response);
    }

    @PostMapping("/createMenu")
    public ApiResult createMenu(){
        zbWxMpService.createMenu();
        return ApiResult.ok();
    }
}
